package com.xinxing.city.recovery.net;

import android.util.Log;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinxing.city.recovery.Constant;
import com.xinxing.city.recovery.RecoveryApplication;

import java.io.IOException;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 晶 on 2016/3/17.
 */
public class RetroFactory {

    private static Retrofit jsonRetrofit = new Retrofit.Builder()
            .baseUrl(Constant.BaseUrl)
            .addConverterFactory(JacksonConverterFactory.create())
            .client(genericClient())
            .build();

    private static Retrofit stringRetrofit = new Retrofit.Builder()
            .baseUrl(Constant.BaseUrl)
            .addConverterFactory(ScalarsConverterFactory.create())
            .build();

    public static ApiServer getJsonService() {
        ApiServer service = jsonRetrofit.create(ApiServer.class);
        return service;
    }

    public static ApiServer getJsonService(String url) {
        Retrofit jsonRetrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(JacksonConverterFactory.create())
                .client(genericClient())
                .build();
        ApiServer service = jsonRetrofit.create(ApiServer.class);
        return service;
    }

    public static ApiServer getStringService() {
        ApiServer service = stringRetrofit.create(ApiServer.class);
        return service;
    }

    public static RequestBody getJSON(Map<String, Object> paramsMap) {
        ObjectMapper mapper = new ObjectMapper();
        String strEntity = "";
        try {
            strEntity = mapper.writeValueAsString(paramsMap);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
    }

    public static RequestBody getJSON2(Map<String, String> paramsMap) {
        ObjectMapper mapper = new ObjectMapper();
        String strEntity = "";
        try {
            strEntity = mapper.writeValueAsString(paramsMap);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
    }

    public static OkHttpClient genericClient() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                Log.i("RetrofitLog", "retrofitBack = " + message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request()
                                .newBuilder()
//                                .addHeader("Accept", "application/json;charset=utf-8")
                                .addHeader("Auth-Token", RecoveryApplication.getInstance().getToken())
                                .build();
                        return chain.proceed(request);
                    }

                })
                .addInterceptor(loggingInterceptor)
                .build();

        return httpClient;
    }
}
